package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"github.com/go-redis/redis"
	"github.com/jinzhu/gorm"
	"io"
	"os"
	"zk3/models"
)

type VideoController struct {
	beego.Controller
}
type Ret struct {
	count int
	Data  interface{}
}

func (c *VideoController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "GET")

	size, _ := c.GetInt("size", 5)
	page, _ := c.GetInt("page", 1)
	offset := (page - 1) * size

	var data []models.Video
	models.DB.Limit(size).Offset(offset).Find(&data)

	var count int
	models.DB.Model(&data).Count(&count)

	c.Data["json"] = ReturnMsg{"200", "展示成功", Ret{count, data}}
	c.ServeJSON(true)

}
func (c *VideoController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "POST")
	VideoName := c.GetString("VideoName")
	VideoPrice := c.GetString("VideoPrice")
	VideoType := c.GetString("VideoType")
	Collect, _ := c.GetInt("Collect")

	video := models.Video{
		VideoName:  VideoName,
		VideoPrice: VideoPrice,
		VideoType:  VideoType,
		Img:        "",
		Collect:    Collect,
	}

	models.DB.Debug().Create(&video)

	c.Data["json"] = ReturnMsg{"200", "添加成功", video}
	c.ServeJSON(true)

}
func (c *VideoController) Show() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "GET")
	id, _ := c.GetInt("id")

	var data models.Video
	models.DB.Where("id = ?", id).Find(&data)
	c.Data["json"] = ReturnMsg{"200", "展示成功", data}
	c.ServeJSON(true)

}
func (c *VideoController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "GET")
	id, _ := c.GetInt("id")
	user := models.Video{
		Model: gorm.Model{ID: uint(id)},
	}
	models.DB.Where("id = ?", id).Delete(&user)

	c.Data["json"] = ReturnMsg{"200", "删除成功", nil}
	c.ServeJSON(true)
}
func (c *VideoController) Updating() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "GET")
	id, _ := c.GetInt("id")
	VideoName := c.GetString("VideoName")
	VideoPrice := c.GetString("VideoPrice")
	VideoType := c.GetString("VideoType")
	Collect, _ := c.GetInt("Collect")

	video := models.Video{
		Model:      gorm.Model{ID: uint(id)},
		VideoName:  VideoName,
		VideoPrice: VideoPrice,
		VideoType:  VideoType,
		Collect:    Collect,
	}

	models.DB.Debug().Model(&video).Updates(&video)
	c.Data["json"] = ReturnMsg{"200", "添加成功", video}
	c.ServeJSON(true)
}

func (c *VideoController) Collect() {

	id, _ := c.GetInt("id")
	var video models.Video
	models.DB.Debug().Where("id = ?", id).Find(&video)

	Video := models.Video{

		Model:   gorm.Model{ID: uint(id)},
		Collect: video.Collect + 1,
	}
	models.DB.Debug().Model(&video).Updates(&Video)
	models.Client.ZAdd("VideoCollect", redis.Z{
		Score:  float64(Video.Collect),
		Member: Video.ID,
	})
	c.Data["json"] = ReturnMsg{"200", "收藏成功", Video}
	c.ServeJSON(true)
}
func (c *VideoController) Onload() {

	file, m, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = ReturnMsg{"500", "错误", nil}
		c.ServeJSON(true)
		return
	}

	if m.Size > 2*1024*1024 {
		c.Data["json"] = ReturnMsg{"200", "图片过大请重新加入", nil}
		c.ServeJSON(true)
		return
	}

	//创建
	create, err := os.Create("C:\\Users\\张永虹\\Desktop\\zk3\\zk3\\static\\img/" + m.Filename)
	if err != nil {
		c.Data["json"] = ReturnMsg{"500", "错误", nil}
		c.ServeJSON(true)
		return
	}

	_, err = io.Copy(create, file)
	if err != nil {
		c.Data["json"] = ReturnMsg{"500", "错误", nil}
		c.ServeJSON(true)
		return
	}

	imgURL := "http://127.0.0.1:8081/static/img/" + m.Filename
	c.Data["json"] = ReturnMsg{"200", "上传成功", imgURL}
	c.ServeJSON(true)

}
